package rexsee.core.transportation;

import java.io.File;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import rexsee.core.lang.RexseeLanguage;
import android.content.Context;

public class RexseeAdvanceDownload extends RexseeDownload
		implements
			JavascriptInterface {

	public static final String INTERFACE_NAME = "AdvanceDownload";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return new RexseeAdvanceDownload(childBrowser);
	}

	public RexseeAdvanceDownload(Browser browser) {
		super(browser);
	}

	public String getPrivateDownloadPath(String url) {
		if (url.endsWith("/")) {
			mBrowser.exception(getInterfaceName(), url + ":Error on extracting the file name from the url.");
			return null;
		}
		String fileName = url.substring(url.lastIndexOf("/") + 1);
		if (fileName == null || fileName.equals("")) {
			mBrowser.exception(getInterfaceName(), url + ":Error on extracting the file name from the url.");
			return null;
		}
		return "file://" + mContext.getDir(DOWNLOAD_DIRECTORY, Context.MODE_PRIVATE).getAbsolutePath() + "/" + fileName;
	}

	public void clearPrivateDownloads() {
		try {
			File dir = mContext.getDir(DOWNLOAD_DIRECTORY, Context.MODE_PRIVATE);
			String[] list = dir.list();
			for (int i = 0; i < list.length; i++) {
				File file = new File(dir.getAbsolutePath() + "/" + list[i]);
				file.delete();
			}
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
		}
	}
	public String getPrivateDownloads() {
		try {
			File dir = mContext.getDir(DOWNLOAD_DIRECTORY, Context.MODE_PRIVATE);
			String rtn = "";
			String[] list = dir.list();
			for (int i = 0; i < list.length; i++) {
				String f = "file://" + dir.getAbsolutePath() + "/" + list[i];
				rtn += (i == 0) ? "\"" + f + "\"" : ",\"" + f + "\"";
			}
			return "[" + rtn + "]";
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
			return "[]";
		}
	}
	public void privateDownload(String url) {
		privateDownload(url, true);
	}
	public void privateDownload(String url, final boolean cancelable) {
		DownloadFile downloadFile = new DownloadFile();
		downloadFile.url = mBrowser.urlListeners.getAbsoluteUrl(url);
		downloadFile.path = getPrivateDownloadPath(downloadFile.url);
		download(mBrowser, downloadFile, new DownloadProgressListener() {
			@Override
			public void run(String url, String path, int status, long totalSize, long downloadedSize, String message, final Runnable stopTheDownload) {
				if (status == DOWNLOAD_PROGRESS_CONNECTING) {
					if (cancelable) {
						mBrowser.progressDialog.showWithRunnable(RexseeLanguage.PROGRESS_CONNECT, stopTheDownload);
					} else {
						mBrowser.progressDialog.show(RexseeLanguage.PROGRESS_CONNECT);
					}
				} else if (status == DOWNLOAD_PROGRESS_TIMEOUT) {
					mBrowser.progressDialog.hide();
				} else if (status == DOWNLOAD_PROGRESS_EXCEPTION) {
					mBrowser.progressDialog.hide();
				} else if (status == DOWNLOAD_PROGRESS_ONGOING) {
					String progress = (totalSize < 0) ? "" : (downloadedSize * 100 / totalSize) + "%";
					String fileName = url.substring(url.lastIndexOf("/") + 1);
					if (cancelable) {
						mBrowser.progressDialog.showWithRunnable(RexseeLanguage.PROGRESS_DOWNLOAD + fileName + "......" + progress + " ", stopTheDownload);
					} else {
						mBrowser.progressDialog.show(RexseeLanguage.PROGRESS_DOWNLOAD + fileName + "......" + progress + " ");
					}
				} else if (status == DOWNLOAD_PROGRESS_DONE) {
					mBrowser.progressDialog.hide();
				}
			}
		});
	}
	public void privateDownloadWithCallback(String url) {
		DownloadFile downloadFile = new DownloadFile();
		downloadFile.url = mBrowser.urlListeners.getAbsoluteUrl(url);
		downloadFile.path = getPrivateDownloadPath(downloadFile.url);
		download(mBrowser, downloadFile, new DownloadProgressListener() {
			@Override
			public void run(String url, String path, int status, long totalSize, long downloadedSize, String message, final Runnable stopTheDownload) {
				mBrowser.eventList.run(EVENT_ONDOWNLOADPROGRESSCHANGED, new String[]{url, path, String.valueOf(totalSize), String.valueOf(downloadedSize)});
			}
		});
	}

}
